package cl.movix.model.customer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import cl.movix.model.products.IProduct;
import cl.movix.model.properties.IProperty;
import cl.movix.model.properties.Property;
import cl.movix.model.properties.NoSuchPropertyException;

public class Customer implements ICustomer {

	/** The user unique ID */
	private long id;

	/** The customer-custom properties :) */
	private HashMap<String, IProperty> properties;

	/** THe customer's products */
	private List<IProduct> products;

	/**
	 * This constructor initialize the array properties and add the given
	 * Standard properties for the customer.
	 * 
	 */
	public Customer() {

		/* The property and product's arrays are initialized */
		this.properties = new HashMap<String, IProperty>();
		this.products = new ArrayList<IProduct>();
	}

	/**
	 * @param properties
	 */
	public Customer(List<IProperty> properties) {
		this();
		this.setProperties(properties);
	}

	@Override
	public long getId() {
		return this.id;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

	@Override
	public List<IProduct> getProducts() {
		return this.products;
	}

	@Override
	public IProperty getProperty(String name) throws NoSuchPropertyException {
		return this.properties.get(name);
	}

	@Override
	public void setProperty(IProperty property) {
		this.properties.put(property.getName(), property);
	}

	@Override
	public List<IProperty> getProperties() {

		List<IProperty> result = new ArrayList<IProperty>();
		result.addAll(this.properties.values());
		return result;
	}

	@Override
	public void setProperties(IProperty[] properties) {

		this.setProperties(Arrays.asList(properties));
	}

	@Override
	public void setProperties(List<IProperty> properties) {
		
		for (IProperty property : properties) {
			this.properties.put(property.getName(), property);
		}
		
	}

	@Override
	public String toString() {
		return this.id + this.properties.toString();
	}
}
